package com.shan.domm.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shan.domm.common.BaseResponse;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.common.ResultUtils;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.mapper.RoomMapper;
import com.shan.domm.model.domain.DTOs.PageParams;
import com.shan.domm.model.domain.Room;
import com.shan.domm.model.domain.User;
import com.shan.domm.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/room")
@Slf4j
public class RoomController {

    @Resource
    private RoomService roomService;

    @Resource
    private RoomMapper roomMapper;


    @PostMapping(value="/addroom")
    public BaseResponse<String> addRoom(@RequestBody Room room){
        if(room==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Boolean b = roomService.addRoom(room);
        if(b){
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"添加失败");
    }

    @PostMapping(value="/deleteroom")
    public BaseResponse<String> deleteRoom(@RequestBody Room room){
        if(room==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Integer i = roomService.deleteRoom(room);
        if(i>0){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"删除失败");

    }

    @PostMapping(value="/updateroom")
    public BaseResponse<String> updateRoom(@RequestBody Room room){
        if(room==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Integer i = roomService.updateRoom(room);
        if(i>0){
            return ResultUtils.success("更新成功");
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"删除失败");
    }

    @PostMapping(value="/searchroom")
    public BaseResponse<List<Room>> searchRoom(String dormNumber){
        if(dormNumber==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Room room = roomService.searchRoom(dormNumber);
        List<Room> room1=new ArrayList<>();
        room1.add(room);
        return ResultUtils.success(room1);
    }

    @PostMapping("/roomList")
    public BaseResponse<Page<Room>> listPage(Integer current,Integer size){
        if(current==null||size==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Page<Room> page=new Page<>(current,size);
        Page<Room> pageList=roomMapper.selectPage(page,null);
        return ResultUtils.success(pageList);
    }
}
